# -*- coding: utf-8 -*-

#   Copyright (C) 2011  P.L. Lucas, P.L. del Castillo
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk
import pango
from picture import *
from fx import *
from transitions import *

class PictureDialog:
	def __init__(self, parent, picture):
		self.picture=picture
		self.dialog= gtk.Dialog("Change picture properties", parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
		self.area_init()
		self.dialog.vbox.pack_start(self.area)
		res=self.dialog.run()
		if res==gtk.RESPONSE_ACCEPT:
			self.picture.set_duration(float(self.duration_entry.get_text()))
			if self.picture.transition!=None:
				self.picture.transition.set_duration(float(self.duration_transition_entry.get_text()))
			it=self.liststore.get_iter_first()
			if None==it:
				self.picture.effect=[FX(self.picture.videoProperties, self.picture.duration)]
			else:
				self.picture.effect=[]
			while it!=None:
				self.picture.effect.append(self.liststore.get_value(it, 1))
				it=self.liststore.iter_next(it)
		self.dialog.destroy()
		
	def area_init(self):
		self.area=gtk.HBox()
		area1=gtk.VBox()
		area2=gtk.VBox()
		self.area.pack_start(area1, False, False)
		self.area.pack_start(area2)
		label=None
		if self.picture.get_path()!=None:
			label=gtk.Label('Path: '+self.picture.get_path())
		else:
			red, green, blue =self.picture.get_color()
			label=gtk.Label('Color: #{red:X} {green:X} {blue:X}'.format(red=int(red*256), green=int(green*256), blue=int(blue*256)))
		label.set_line_wrap(True)
		label.set_selectable(True)
		label.set_ellipsize(pango.ELLIPSIZE_MIDDLE)
		area1.pack_start(label, False, False)
		image=gtk.Image()
		image.set_from_pixbuf(self.picture.get_pixbuf(200,150))
		area1.pack_start(image, False, False)
		label=gtk.Label('Duration: ')
		area1.pack_start(label, False, False)
		self.duration_entry=gtk.Entry()
		self.duration_entry.set_text(str(self.picture.duration))
		area1.pack_start(self.duration_entry, False, False)
		area1.pack_start(self.transition_init(), True, True)
		self.list_init()
		area2.pack_start(self.treeview)
		area3=gtk.HBox()
		area2.pack_end(area3,False, False)
		add_button=gtk.Button('Add', gtk.STOCK_ADD)
		add_button.connect("clicked", self.add_callback, "add")
		area3.pack_start(add_button,False, False)
		remove_button=gtk.Button('Remove', gtk.STOCK_DELETE)
		remove_button.connect("clicked", self.remove_callback, "remove")
		area3.pack_start(remove_button,False, False)
		up_button=gtk.Button('Up', gtk.STOCK_GO_UP)
		up_button.connect("clicked", self.up_callback, "up")
		area3.pack_start(up_button,False, False)
		down_button=gtk.Button('Down', gtk.STOCK_GO_DOWN)
		down_button.connect("clicked", self.down_callback, "down")
		area3.pack_start(down_button,False, False)
		config_button=gtk.Button('Configure', gtk.STOCK_PROPERTIES)
		config_button.connect("clicked", self.config_effect_callback, "down")
		area3.pack_start(config_button,False, False)
		self.area.show_all()
	
	def transition_init(self):
		frame=gtk.Frame('Transition')
		vbox=gtk.VBox()
		frame.add(vbox)
		label=gtk.Label('Duration')
		vbox.pack_start(label, False, False)
		self.duration_transition_entry=gtk.Entry()
		if self.picture.transition!=None:
			self.duration_transition_entry.set_text(str(self.picture.transition.duration))
		else:
			self.duration_transition_entry.set_text('0')
		vbox.pack_start(self.duration_transition_entry, False, False)
		combobox = gtk.combo_box_new_text()
		vbox.pack_start(combobox, False, False)
		i=1
		found_ok=False
		combobox.append_text('Select transition')
		lista=[]
		for k,v in transitions_list().iteritems():
			lista.append([k, v])
		for x in sorted(lista, None, lambda x: x[0]):
			#for k,v in transitions_list().iteritems():
			k=x[0]
			v=x[1]
			combobox.append_text(k)
			if self.picture.transition!=None and self.picture.transition.class_base==v:
				found_ok=True
			elif self.picture.transition==None and v==None:
				found_ok=True
			if not found_ok:
				i+=1
		combobox.set_active(i)
		combobox.connect('changed', self.transition_callback)
		config_button=gtk.Button('Configure', gtk.STOCK_PROPERTIES)
		config_button.connect("clicked", self.config_callback, "down")
		vbox.pack_start(config_button,False, False)
		frame.show_all()
		return frame
	
	def list_init(self):
		self.liststore = gtk.ListStore(str, object)
		self.list_fill()
		self.treeview = gtk.TreeView(self.liststore)
		self.treeview.set_reorderable(True)
		tvcolumn = gtk.TreeViewColumn('Effect')
		self.treeview.append_column(tvcolumn)
		self.cell = gtk.CellRendererText()
		tvcolumn.pack_start(self.cell, True)
		tvcolumn.add_attribute(self.cell, 'text', 0)
		self.treeview.connect("row-activated", self.properties_callback, None)
	
	def list_fill(self):
		for f in self.picture.effect:
			piter = self.liststore.append([f.get_name(), f])
	
	def add_callback(self, widget, data):
		dialog=AddDialog(self.dialog)
		if dialog.effect!=None:
			#Se añade el nuevo efecto
			f=dialog.effect(self.picture.videoProperties, self.picture.duration)
			self.picture.effect.append(f)
			self.liststore.clear()
			self.list_fill()
	
	def remove_callback(self, widget, data):
		treeselection = self.treeview.get_selection()
		(model, itera) = treeselection.get_selected()
		if itera!=None:
			v=self.liststore.get_value(itera, 1)
			self.picture.effect.remove(v)
			self.liststore.remove(itera)
	
	def up_callback(self, widget, data):
		treeselection = self.treeview.get_selection()
		(model, itera) = treeselection.get_selected()
		if itera!=None:
			it=self.liststore.get_iter_first()
			last_it=it
			while it!=None:
				if self.liststore.get_path(it)==self.liststore.get_path(itera):
					self.liststore.swap(itera,last_it)
					break
				last_it=it
				it=self.liststore.iter_next(it)

	def down_callback(self, widget, data):
		treeselection = self.treeview.get_selection()
		(model, itera) = treeselection.get_selected()
		if itera!=None:
			it=self.liststore.iter_next(itera)
			if it!=None:
				self.liststore.swap(itera,it)
	
	def config_callback(self, widget, data):
		if self.picture.transition!=None:
			self.picture.transition.show_menu(self.dialog)
	
	def config_effect_callback(self, widget, data):
		treeselection = self.treeview.get_selection()
		(model, itera) = treeselection.get_selected()
		if itera!=None:
			effect=self.liststore.get_value(itera, 1)
			self.show_menu_config_effect(effect)
	
	def transition_callback(self, combobox):
		model = combobox.get_model()
		index = combobox.get_active()
		if index:
			self.picture.transition=transitions_list()[model[index][0]]
			if self.picture.transition!=None:
				self.picture.transition=self.picture.transition(self.picture.videoProperties, float(self.duration_transition_entry.get_text()))
		return
	
	def properties_callback(self, treeview, path, view_column, user_data):
		effect=self.liststore.get_value(self.liststore.get_iter(path), 1)
		self.show_menu_config_effect(effect)
	
	def show_menu_config_effect(self, effect):
		effect.show_menu(self.dialog)
		self.liststore.clear()
		self.list_fill()


class AddDialog:
	def __init__(self, parent):
		self.dialog= gtk.Dialog("Add effect", parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
		self.area_init()
		self.dialog.vbox.pack_start(self.area)
		res=self.dialog.run()
		self.effect=None
		if res==gtk.RESPONSE_ACCEPT:
			treeselection = self.treeview.get_selection()
			(model, itera) = treeselection.get_selected()
			if itera!=None:
				self.effect=self.liststore.get_value(itera, 1)
		self.dialog.destroy()
		
	def area_init(self):
		self.area=gtk.VBox()
		self.list_init()
		self.area.pack_start(self.treeview)
		self.area.show_all()
	
	def list_init(self):
		self.liststore = gtk.ListStore(str, object)
		lista=[]
		for k,f in fx_list().iteritems():
			lista.append([k, f])
			#piter = self.liststore.append([k, f])
		for k in sorted(lista, None, lambda x: x[0]):
			piter = self.liststore.append(k)
		self.treeview = gtk.TreeView(self.liststore)
		tvcolumn = gtk.TreeViewColumn('Effect')
		tvcolumn.set_sort_column_id(0)
		tvcolumn.set_sort_order(gtk.SORT_ASCENDING)
		self.treeview.append_column(tvcolumn)
		self.cell = gtk.CellRendererText()
		tvcolumn.pack_start(self.cell, True)
		tvcolumn.add_attribute(self.cell, 'text', 0)
